Imports System.Data.SqlClient
Imports AppsUtility.EncDec
Imports System.Configuration

Public Class frmServiceType
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents txtName As System.Windows.Forms.TextBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents txtCode As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents dgService As System.Windows.Forms.DataGridView
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents TypeId As System.Windows.Forms.DataGridViewTextBoxColumn
    Friend WithEvents TypeName As System.Windows.Forms.DataGridViewTextBoxColumn
    Friend WithEvents cmdSave As DevComponents.DotNetBar.ButtonX
    Friend WithEvents cmdExit As DevComponents.DotNetBar.ButtonX
    Friend WithEvents GroupPanel1 As DevComponents.DotNetBar.Controls.GroupPanel
    Friend WithEvents TypeCode As System.Windows.Forms.DataGridViewTextBoxColumn
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox()
        Me.cmdSave = New DevComponents.DotNetBar.ButtonX()
        Me.cmdExit = New DevComponents.DotNetBar.ButtonX()
        Me.txtCode = New System.Windows.Forms.TextBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.dgService = New System.Windows.Forms.DataGridView()
        Me.TypeId = New System.Windows.Forms.DataGridViewTextBoxColumn()
        Me.TypeName = New System.Windows.Forms.DataGridViewTextBoxColumn()
        Me.TypeCode = New System.Windows.Forms.DataGridViewTextBoxColumn()
        Me.txtName = New System.Windows.Forms.TextBox()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.GroupPanel1 = New DevComponents.DotNetBar.Controls.GroupPanel()
        Me.GroupBox1.SuspendLayout()
        CType(Me.dgService, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.GroupPanel1.SuspendLayout()
        Me.SuspendLayout()
        '
        'GroupBox1
        '
        Me.GroupBox1.BackColor = System.Drawing.Color.Transparent
        Me.GroupBox1.Controls.Add(Me.cmdSave)
        Me.GroupBox1.Controls.Add(Me.cmdExit)
        Me.GroupBox1.Controls.Add(Me.txtCode)
        Me.GroupBox1.Controls.Add(Me.Label1)
        Me.GroupBox1.Controls.Add(Me.dgService)
        Me.GroupBox1.Controls.Add(Me.txtName)
        Me.GroupBox1.Controls.Add(Me.Label2)
        Me.GroupBox1.Controls.Add(Me.Label4)
        Me.GroupBox1.Controls.Add(Me.Label3)
        Me.GroupBox1.Location = New System.Drawing.Point(28, 3)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(449, 327)
        Me.GroupBox1.TabIndex = 9
        Me.GroupBox1.TabStop = False
        '
        'cmdSave
        '
        Me.cmdSave.AccessibleRole = System.Windows.Forms.AccessibleRole.PushButton
        Me.cmdSave.Font = New System.Drawing.Font("Calibri", 11.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.cmdSave.ForeColor = System.Drawing.Color.FromArgb(CType(CType(0, Byte), Integer), CType(CType(0, Byte), Integer), CType(CType(64, Byte), Integer))
        Me.cmdSave.Location = New System.Drawing.Point(246, 93)
        Me.cmdSave.Name = "cmdSave"
        Me.cmdSave.Size = New System.Drawing.Size(82, 27)
        Me.cmdSave.TabIndex = 277
        Me.cmdSave.Text = "Save"
        '
        'cmdExit
        '
        Me.cmdExit.AccessibleRole = System.Windows.Forms.AccessibleRole.PushButton
        Me.cmdExit.Font = New System.Drawing.Font("Calibri", 11.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.cmdExit.ForeColor = System.Drawing.Color.FromArgb(CType(CType(0, Byte), Integer), CType(CType(0, Byte), Integer), CType(CType(64, Byte), Integer))
        Me.cmdExit.Location = New System.Drawing.Point(336, 93)
        Me.cmdExit.Name = "cmdExit"
        Me.cmdExit.Size = New System.Drawing.Size(82, 27)
        Me.cmdExit.TabIndex = 276
        Me.cmdExit.Text = "Exit"
        '
        'txtCode
        '
        Me.txtCode.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtCode.Location = New System.Drawing.Point(301, 51)
        Me.txtCode.MaxLength = 10
        Me.txtCode.Name = "txtCode"
        Me.txtCode.Size = New System.Drawing.Size(117, 26)
        Me.txtCode.TabIndex = 1
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.BackColor = System.Drawing.Color.Transparent
        Me.Label1.Location = New System.Drawing.Point(302, 30)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(120, 18)
        Me.Label1.TabIndex = 9
        Me.Label1.Text = "Service Type Code"
        '
        'dgService
        '
        Me.dgService.AllowUserToAddRows = False
        Me.dgService.AllowUserToDeleteRows = False
        Me.dgService.AllowUserToOrderColumns = True
        Me.dgService.AllowUserToResizeRows = False
        Me.dgService.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
        Me.dgService.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.TypeId, Me.TypeName, Me.TypeCode})
        Me.dgService.EditMode = System.Windows.Forms.DataGridViewEditMode.EditProgrammatically
        Me.dgService.Location = New System.Drawing.Point(33, 138)
        Me.dgService.MultiSelect = False
        Me.dgService.Name = "dgService"
        Me.dgService.RowHeadersVisible = False
        Me.dgService.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect
        Me.dgService.Size = New System.Drawing.Size(385, 165)
        Me.dgService.TabIndex = 13
        '
        'TypeId
        '
        Me.TypeId.DataPropertyName = "TypeId"
        Me.TypeId.HeaderText = "TypeId"
        Me.TypeId.Name = "TypeId"
        Me.TypeId.Visible = False
        '
        'TypeName
        '
        Me.TypeName.DataPropertyName = "TypeName"
        Me.TypeName.HeaderText = "Service Type Name"
        Me.TypeName.Name = "TypeName"
        Me.TypeName.Width = 230
        '
        'TypeCode
        '
        Me.TypeCode.DataPropertyName = "TypeCode"
        Me.TypeCode.HeaderText = "Service Type Code"
        Me.TypeCode.Name = "TypeCode"
        Me.TypeCode.Width = 150
        '
        'txtName
        '
        Me.txtName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtName.Location = New System.Drawing.Point(28, 51)
        Me.txtName.MaxLength = 50
        Me.txtName.Name = "txtName"
        Me.txtName.Size = New System.Drawing.Size(232, 26)
        Me.txtName.TabIndex = 0
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.BackColor = System.Drawing.Color.Transparent
        Me.Label2.Location = New System.Drawing.Point(30, 30)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(125, 18)
        Me.Label2.TabIndex = 9
        Me.Label2.Text = "Service Type Name"
        '
        'Label4
        '
        Me.Label4.AutoSize = True
        Me.Label4.BackColor = System.Drawing.Color.Transparent
        Me.Label4.Font = New System.Drawing.Font("Calibri", 11.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label4.ForeColor = System.Drawing.Color.Red
        Me.Label4.Location = New System.Drawing.Point(157, 30)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(15, 18)
        Me.Label4.TabIndex = 9
        Me.Label4.Text = "*"
        '
        'Label3
        '
        Me.Label3.AutoSize = True
        Me.Label3.BackColor = System.Drawing.Color.Transparent
        Me.Label3.Font = New System.Drawing.Font("Calibri", 11.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label3.ForeColor = System.Drawing.Color.Red
        Me.Label3.Location = New System.Drawing.Point(419, 30)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(15, 18)
        Me.Label3.TabIndex = 9
        Me.Label3.Text = "*"
        '
        'GroupPanel1
        '
        Me.GroupPanel1.CanvasColor = System.Drawing.SystemColors.Control
        Me.GroupPanel1.ColorSchemeStyle = DevComponents.DotNetBar.eDotNetBarStyle.Office2007
        Me.GroupPanel1.Controls.Add(Me.GroupBox1)
        Me.GroupPanel1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.GroupPanel1.Location = New System.Drawing.Point(0, 0)
        Me.GroupPanel1.Name = "GroupPanel1"
        Me.GroupPanel1.Size = New System.Drawing.Size(516, 378)
        '
        '
        '
        Me.GroupPanel1.Style.BackColor2SchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelBackground2
        Me.GroupPanel1.Style.BackColorGradientAngle = 90
        Me.GroupPanel1.Style.BackColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelBackground
        Me.GroupPanel1.Style.BorderBottom = DevComponents.DotNetBar.eStyleBorderType.Solid
        Me.GroupPanel1.Style.BorderBottomWidth = 1
        Me.GroupPanel1.Style.BorderColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelBorder
        Me.GroupPanel1.Style.BorderLeft = DevComponents.DotNetBar.eStyleBorderType.Solid
        Me.GroupPanel1.Style.BorderLeftWidth = 1
        Me.GroupPanel1.Style.BorderRight = DevComponents.DotNetBar.eStyleBorderType.Solid
        Me.GroupPanel1.Style.BorderRightWidth = 1
        Me.GroupPanel1.Style.BorderTop = DevComponents.DotNetBar.eStyleBorderType.Solid
        Me.GroupPanel1.Style.BorderTopWidth = 1
        Me.GroupPanel1.Style.CornerDiameter = 4
        Me.GroupPanel1.Style.CornerType = DevComponents.DotNetBar.eCornerType.Rounded
        Me.GroupPanel1.Style.TextAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Center
        Me.GroupPanel1.Style.TextColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelText
        Me.GroupPanel1.Style.TextLineAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Near
        Me.GroupPanel1.TabIndex = 10
        Me.GroupPanel1.Text = "Service Type"
        '
        'frmServiceType
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(7, 19)
        Me.BackColor = System.Drawing.SystemColors.ActiveCaption
        Me.ClientSize = New System.Drawing.Size(516, 378)
        Me.ControlBox = False
        Me.Controls.Add(Me.GroupPanel1)
        Me.Font = New System.Drawing.Font("Calibri", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
        Me.Name = "frmServiceType"
        Me.ShowInTaskbar = False
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox1.PerformLayout()
        CType(Me.dgService, System.ComponentModel.ISupportInitialize).EndInit()
        Me.GroupPanel1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region


    Dim SerTypeId As Integer

    Dim util As New AppsUtility.EncDec
    Public strCon As String = util.toDecrypt(System.Configuration.ConfigurationManager.AppSettings("SQLString"))
    Dim SqlCon As New SqlConnection(strCon)


    Private Sub cmdSave_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Try
            Dim StrSql As String
            Dim count As Integer = 0
            Dim dtable As DataTable
            Dim cmd As SqlCommand

            If txtName.Text.Trim = "" Or txtCode.Text.Trim = "" Then
                MsgBox("Enter Service Tyep and Service Code", 16, "Inventory Manager")
                txtName.Focus()
            Else

                If cmdSave.Text = "&Save" Then

                    StrSql = "Select Count(*) from ServiceType where TypeName='" & txtName.Text.Trim & "' and TypeCode='" & txtCode.Text.Trim & "'"
                    cmd = New SqlCommand(StrSql, SqlCon)
                    cmd.CommandType = CommandType.Text

                    SqlCon.Open()
                    count = cmd.ExecuteScalar
                    If count = 0 Then
                        StrSql = "Insert into ServiceType(TypeName,TypeCode) values ('" & txtName.Text.Trim & "', '" & txtCode.Text.Trim & "')"
                        cmd = New SqlCommand(StrSql, SqlCon)
                        cmd.CommandType = CommandType.Text
                        cmd.ExecuteNonQuery()

                        StrSql = "Select TypeId,TypeName,TypeCode from ServiceType"
                        cmd = New SqlCommand(StrSql, SqlCon)
                        cmd.CommandType = CommandType.Text
                        dtable = New DataTable
                        dtable.Load(cmd.ExecuteReader)
                        dgService.DataSource = dtable

                        MsgBox("Service Type Saved Successfully", 64, "Inventory Manager")
                        txtCode.Clear()
                        txtName.Clear()
                        txtName.Focus()

                    Else
                        MsgBox("Service Tyep or Service Code existing", 16, "Inventory Manager")
                        txtName.Focus()
                    End If
                    SqlCon.Close()
                Else

                    StrSql = "Update ServiceType set TypeName='" & txtName.Text.Trim & "', TypeCode='" & txtCode.Text.Trim & "' where TypeId=" & SerTypeId
                    cmd = New SqlCommand(StrSql, SqlCon)
                    cmd.CommandType = CommandType.Text
                    SqlCon.Open()
                    cmd.ExecuteNonQuery()

                    StrSql = "Select TypeId,TypeName,TypeCode from ServiceType"
                    cmd.CommandType = CommandType.Text
                    dtable = New DataTable
                    dtable.Load(cmd.ExecuteReader)
                    dgService.DataSource = dtable
                    SerTypeId = 0
                    cmdSave.Text = "&Save"
                    cmdExit.Text = "E&xit"
                    SqlCon.Close()

                    MsgBox("Service Type Updated Successfully", 64, "Inventory Manager")
                    txtCode.Clear()
                    txtName.Clear()
                    txtName.Focus()
                End If

            End If


        Catch ex As Exception
            MsgBox(ex.ToString, 16, "Inventory Manager")
        End Try

    End Sub

    Private Sub cmdExit_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        If cmdExit.Text = "&Cancel" Then
            SerTypeId = 0
            cmdSave.Text = "&Save"
            cmdExit.Text = "E&xit"
        Else
            Me.Close()
        End If

    End Sub


    Private Sub dgService_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgService.CellDoubleClick
        Try
            cmdSave.Text = "&Update"
            cmdExit.Text = "&Cancel"
            SerTypeId = dgService.SelectedRows(0).Cells(0).Value
            txtName.Text = dgService.SelectedRows(0).Cells(1).Value
            txtCode.Text = dgService.SelectedRows(0).Cells(2).Value

        Catch ex As Exception
            MsgBox(ex.ToString, 16, "Inventory Manager")
        End Try
    End Sub

    Private Sub frmServiceType_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim StrSql As String
        Dim cmd As SqlCommand
        Dim dtable As New DataTable

        StrSql = "Select TypeId,TypeName,TypeCode from ServiceType"
        cmd = New SqlCommand(StrSql, SqlCon)
        cmd.CommandType = CommandType.Text
        SqlCon.Open()
        dtable.Load(cmd.ExecuteReader)
        dgService.DataSource = dtable
        SqlCon.Close()
    End Sub
End Class
